<?php
define('PAGE_CACHE_DIR',DIR_CACHE.'pages/');
define('PAGE_CACHE_ON',true);
//ECore::del_page_cache();

class ECore{
	static $current = false,$page = false,$page_cache_file='',$extraHeader = '',$extraFooter = '',$extraHeaderCSS = '',$extraHeaderJS = '';
	static function Run()
	{
		ECLib::CheckDir(PAGE_CACHE_DIR);

		if(isset($_REQUEST['page']) && $_REQUEST['page'] != '')
		{
			if($_REQUEST['page'] == "home")
			{
				Url::redirect_url(WEB_DIR,301);
			}
			
			$page_name = strtolower($_REQUEST['page']);
		}
		else
		{
			$page_name = 'home';//$page_name = 'cpc_ad';
		}
		
		ECore::$page_cache_file = PAGE_CACHE_DIR.$page_name.'.php';

		if(Url::get('refresh_page')==1){
			self::del_page_cache($page_name);
		}
		
		if(Url::get('refresh_page')!=1 && PAGE_CACHE_ON && file_exists(ECore::$page_cache_file))
		{
            require_once ECore::$page_cache_file;
		}
		else{
			$re = DB::query('SELECT id, name, title, layout  FROM page WHERE name="'.addslashes($page_name).'"',__LINE__.__FILE__);
			if($re){ECore::$page=mysql_fetch_assoc($re);}
			
			if(!ECore::$page){Url::redirect_url(WEB_DIR,301);}
			require_once ROOT_PATH.'core/ECGen.php';
			ECGen::PageGenerate();
		}
	}
	
	static function update_page($ids)//$ids là danh sách id dạng "1,2,3";
	{
		$re=DB::query('SELECT name FROM page WHERE id IN ('.$ids.')',__LINE__.__FILE__);
		
		$pages=array();
		
		if($re)
		{
			while ($page=mysql_fetch_assoc($re)){
				if($page && $page['name']){
					self::del_page_cache($page['name']);
				}
			}
		}
		
		return true;  	
	}
	
	static function update_page_by_name($page_name)
	{
		if($page_name != '')
		{
			self::del_page_cache($page_name);
		}
		
		return true;  	
	}
	
	static function update_all_page(){
		$re		=	DB::query('SELECT name FROM page',__LINE__.__FILE__);
		$pages	=	array();
		
		if($re)
		{
			while ($page = mysql_fetch_assoc($re))
			{
				if($page && $page['name'])
				{
					self::del_page_cache($page['name']);
				}
			}
		}
		
		return true;  	
	}
	
	static function del_page_cache($page = '')
	{
		if($page != '')
		{
			if(is_array(CGlobal::$my_server))
			{
				foreach (CGlobal::$my_server as $server)
				{
					$link = "http://{$server}/?trigger=1&page_cache_file={$page}";	
					
					if(@fopen($link,"r"))
					{
						//if(DEBUG){echo "run service in $link<br>";}					
					}
					else
					{
						if(DEBUG){echo "error in $link<br>";}	
					}
				}				
			}
			
			return true;
  		}//trigger delscache
		elseif(isset($_REQUEST['trigger'])  && isset($_REQUEST['page_cache_file']) && $_REQUEST['trigger'] && $_REQUEST['page_cache_file'])
		{
			$page_cache_file=$_REQUEST['page_cache_file'];
			@unlink(PAGE_CACHE_DIR.$page_cache_file.'.php');
			
			if(DEBUG){
				echo "Deleted Page cache file: {$page_cache_file}";
			}
			
			exit;
		}
	}
}
?>